import SceneDialog from './SceneDialog/SceneDialog.vue';
import QuitOrder from './QuitDialog/QuitDialog.vue';

export default {
    components: {
        SceneDialog,
        QuitOrder
    },
    name: 'OrderManage',
    data() {
        return {
            //
            loading: true,
            // 当前页
            currentPage: 1,
            // 每页显示多少条
            pageSize: 10,
            // 总页数
            total: 1,
            // 现场勘查数据
            selectionForDialog: {},
            // 退单
            quitForDialog: {},
            // 展开收起搜索条件
            expandConditionFlag: false,
            // 现场勘察
            sceneProspectFlag: false,
            // 退单申请
            quitOrderFlag: false,
            // 工单编号
            workOrderNo: '',
            // 工单名称
            workOrderName: '',
            // 工单状态
            workOrderStatus: '',
            orderStatusList: [],
            test: '',
            // 表格数据
            tableData: [],
            multipleSelection: []
        };
    },
    created() {
        // 获取表格数据
        this.orderTableData(1, 10);

        this.$httpExt()
            .get('/api/common/getSelections', {
                pkeyPath: 'BisArguSetting.pssp.orderStatus'
            })
            .then(
                response => {
                    this.orderStatusList = response.data;
                    console.log('字典返回值', response.data);
                },
                response => {
                    console.log('异常', response);
                }
            );
    },
    methods: {
        // 每页显示多少条, 重置current从1开始
        handleSizeChange(val) {
            this.pageSize = val;
            this.currentPage = 1;
            this.orderTableData(this.currentPage, val);
        },
        // 当前页数
        handleCurrentChange(val) {
            this.orderTableData(val, 10);
        },
        formatData(list) {
            // 处理后端数据格式
            let resultFormat = [];
            resultFormat = list.map(item => {
                return {
                    // 地址
                    orderProjectAddress: item.pmsConstructionOrder.orderProjectAddress,
                    // 名称
                    orderProjectName: item.pmsConstructionOrder.orderProjectName,
                    // 面积
                    renovationTotalArea: item.pmsConstructionOrder.renovationTotalArea,
                    // id
                    workOrderId: item.workOrderId,
                    // 工单号
                    workOrderNo: item.workOrderNo,
                    // 工单状态
                    workOrderStatus: item.workOrderStatus,
                    // 年采区域
                    city: item.pmsConstructionOrder.city,
                    // 甲方人
                    followUpEmp: item.pmsConstructionOrder.followUpEmp,
                    // 电话
                    followUpPhone: item.pmsConstructionOrder.followUpPhone,
                    // 退单原因
                    chargebackReason: item.chargebackReason,
                    // 是否可以退单
                    canChargeBack: item.canChargeBack,
                    // 是否可以现场勘查
                    canReconnaissance: item.canReconnaissance
                };
            });
            return resultFormat;
        },
        // 获取表格数据
        orderTableData(current, size) {
            this.$httpExt()
                .post('/api/pmsSupplierWorkOrder/page', {
                    condition: {
                        workOrderNo: this.workOrderNo,
                        workOrderStatus: this.workOrderStatus,
                        pmsConstructionOrder: {
                            orderProjectName: this.workOrderName
                        }
                    },
                    current: current,
                    size: size
                })
                .then(
                    response => {
                        console.log('返回值', response.data);
                        // 表格重新赋值
                        let format = this.formatData(response.data.records);
                        this.loading = false;
                        this.total = response.data.total;
                        this.tableData = format;
                    },
                    response => {
                        Notification.error({
                            title: '异常',
                            message: response.msg
                        });
                    }
                );
        },
        // 查询
        resultQuery() {
            this.orderTableData(1, 10);
        },
        // 重置搜索条件
        resetQuery() {
            this.workOrderStatus = '';
            this.workOrderNo = '';
            this.workOrderName = '';
            this.orderTableData(1, this.pageSize);
        },
        // 选中行
        handleSelectionChange(level) {
            this.multipleSelection = level;
        },
        expandCondition() {
            this.expandConditionFlag = !this.expandConditionFlag;
        },
        // 现场勘察打开
        sceneProspect() {
            // 如果用户未选中，则提示先选择一条数据
            if (this.multipleSelection.length !== 1) {
                alert('请选择一条数据');
                return false;
            }
            this.sceneProspectFlag = true;
            this.selectionForDialog = JSON.parse(JSON.stringify(this.multipleSelection[0]));
        },
        // 关闭
        closeDialog() {
            this.sceneProspectFlag = false;
            this.quitOrderFlag = false;
            this.resultQuery();
        },
        // 退单框打开
        quitOrder() {
            // 如果用户未选中，则提示先选择一条数据
            if (this.multipleSelection.length !== 1) {
                alert('请选择一条数据');
                return false;
            }
            this.quitOrderFlag = true;
            this.quitForDialog = JSON.parse(JSON.stringify(this.multipleSelection[0]));
            console.log('退单数据', this.quitForDialog);
        },
        /**
         * 转义工单状态
         * @param row
         */
        formatterWorkStatus(row) {
            return this.orderStatusList.filter(item => item.value === row.workOrderStatus)[0]['label'];
        },
        /**
         * 下载布局设计
         */
        downDesign() {
            if (this.multipleSelection.length !== 1) {
                this.$message({
                    message: '请选择一条数据',
                    type: 'warning'
                });
                return false;
            }
            console.log('选中的数据', this.multipleSelection[0]);
            console.log('选中的文件Id', this.multipleSelection[0]['workOrderNo']);
            //获取附件名称
            this.$httpExt()
                .post('/api/pmsSupplierWorkOrder/getLayoutPlan', {
                    workOrderNumber: this.multipleSelection[0]['workOrderNo']
                })
                .then(response => {
                    console.log('返回的Id', response.data);
                    let link = document.createElement('a');
                    link.href = process.env.VUE_APP_BASE_URL + 'attachment/download?fileId=' + response.data;
                    link.download = '布局设计';
                    document.body.appendChild(link);
                    link.click();
                    link.remove();
                    window.URL.revokeObjectURL(link.href);
                });
        }
    }
};
